#include "console.h"
#include <Windows.h>
#include <tchar.h>

Console::SMART_PTR Console::This(NULL);

Console::Console(std::wstring strName) : m_pStdOutFile(NULL), m_pStdErrFile(NULL)
{
	AllocConsole();
	SetConsoleTitleW(strName.c_str());
	_tfreopen_s(&m_pStdOutFile, L"CONOUT$", L"a+", stdout);
//	_tfreopen_s(&m_pStdErrFile, L"CONERR$", L"a+", stderr);
}

Console::~Console()
{
	if (m_pStdOutFile)
		fclose(m_pStdOutFile);
	if (m_pStdErrFile)
		fclose(m_pStdErrFile);

	FreeConsole();
}

bool Console::CreateConsole(std::wstring strName /* = L"demo"*/)
{
	if (Console::This.get())
		return false;

	Console::This.reset(new Console(strName));
	return true;
}

bool Console::DestroyConsole()
{
	if (NULL == Console::This.get())
		return false;

	Console::This.reset(NULL);
	return true;
}
